﻿using System;

namespace RFID.BLL
{
    /// <summary>
    /// 操作结果类
    /// </summary>
    public sealed class OperateResult<T>
    {
        private OperateResult()
        {
        }

        /// <summary>
        /// 操作是否成功
        /// </summary>
        public bool Status { get; private set; }

        /// <summary>
        /// 操作失败的详情
        /// </summary>
        public Exception Error { get; private set; }

        /// <summary>
        /// 操作失败的原因
        /// </summary>
        public string Message => Error?.Message;

        /// <summary>
        /// 操作成功返回的数据
        /// </summary>
        public T Data { get; private set; } = default;

        /// <summary>
        /// 操作成功
        /// </summary>
        /// <param name="data">返回数据</param>
        /// <returns></returns>
        public static OperateResult<T> Succeeded(T data)
        {
            return new OperateResult<T>()
            {
                Status = true,
                Data = data
            };
        }

        /// <summary>
        /// 操作异常
        /// </summary>
        /// <param name="message">失败原因</param>
        /// <returns></returns>
        public static OperateResult<T> Failed(Exception exception)
        {
            return new OperateResult<T>()
            {
                Status = false,
                Data = default,
                Error = exception
            };
        }

        /// <summary>
        /// 操作失败
        /// </summary>
        /// <param name="message">失败原因</param>
        /// <returns></returns>
        public static OperateResult<T> Failed(string message)
        {
            return Failed(new Exception(message: message));
        }
    }
}
